const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

//==============1最新动态==========
//业务逻辑
module.exports = function() {
    var router = express.Router();
    //查询最新动态
    router.get('/TheSecondary.html',function(req,res,next){
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
        //获得url中的参数
        var page=urlobj.parse(req.url,true).query.page;
        if(!page)
        {
            page=1;
        }

        var pageNum=6;
        //计算总页数
        db.query('SELECT * FROM classb',function(err,data){
            if(err)
            {

                res.status(400).send('<script>alert("计算总页数失败!");window.location.href="404.html";</script>').end();
            }
            else
            {
                if(data.length==0)
                {
                    res.status(400).send('<script>alert("当前没有数据!");window.location.href="404.html";</script>').end();
                }
                else
                {
                    var conts=data.length;
                    conts=Math.ceil(conts/pageNum);
                    //存总页数
                    req.classAId=conts;
                    //存当前页码
                    req.page=page;

                }
            }
        });

        //加载会员的数据  分页
        db.query('SELECT * FROM  classa ,classb WHERE classa.classAId=classb .classAId ORDER BY classb.classBtime DESC LIMIT '+pageNum*(page-1)+' , '+pageNum+'',function(err,data){
            if(err)
            {
                res.status(500).send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
            }
            else
            {
                if(data.length==0)
                {
                    res.status(400).send('<script>alert("没有数据!");window.location.reload();</script>').end();
                }
                else
                {
                    req.classAName=data;
                }
                next();
            }
        });
    });


    //删除二级分类
    router.get('/TheSecondary.html',function(req,res,next){
        //删除二级分类信息

        //格式化url栏的参数，转换为对像
       // console.log(urlobj.parse(req.url,true).query.act);
        //用户确定要删除，查询当前对应的是否有商品,没有商品就删除成功
       if(req.query.classBId==null||req.query.classBId=="")
       {
           next();
       }else
       {
           if(urlobj.parse(req.url,true).query.act=="delete")
           {
               db.query("SELECT * FROM classb,classc WHERE classb.classBId=classc.classBId and classb.classBId="+ +req.query.classBId+"",function(err,data) {
                   if(err)
                   {

                       res.status(500).send('<script>alert("链表查询出错了!");window.location.href="404.html";</script>').end();

                   }else
                   {
                       if(data.length>0)
                       {
                           console.log(data.length)
                           res.status(500).send('<script>alert("该分类下面存在商品，不能删除");window.location.href="TheSecondary.html";</script>').end()
                       }else if(data.length==0)
                       {
                           db.query("DELETE FROM classb WHERE classBId="+ (+req.query.classBId)+"",function(err,data) {
                               if(err)
                               {
                                   res.status(500).send('<script>alert("删除出错了!");window.location.href="404.html";</script>').end()
                               }else
                               {
                                   res.status(200).send('<script>alert("删除成功！");window.location.href="TheSecondary.html";</script>').end()
                               }
                           });
                       }else
                       {
                           next();
                       }
                   }
               })
           }
       }

    });

   //开始渲染后台首页
    router.get('/TheSecondary.html',function(req,res,next){
        //res.render("admin/PrimaryClassification.ejs",{classa:req.classAName,page:req.classAId});
        res.render("admin/TheSecondary.ejs",{classa:req.classAName,page:req.classAId});
    });
    // router.get('/AddertiaryGoods.html',function(req,res,next){
    //     //res.render("admin/PrimaryClassification.ejs",{classa:req.classAName,page:req.classAId});
    //     res.render("admin/AddertiaryGoods.ejs",{classa:req.classAName});
    // });
    return router;
}